package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("DetectfeatureController")
public class DetectfeatureController extends CmiEmaController {

	@RequestMapping("/listDetectfeatures")
	public ModelAndView listDetectfeatures(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = detectfeaturePaginationService.updatePagination(request, pagination);
		Set<Detectfeature> detectfeatures = detectfeaturePaginationService.getPaginationDetectfeatures(pagination);

		mav.addObject("detectfeatures", detectfeatures);
		mav.addObject("entityName", "detectfeature");
		mav.addObject("pagination", pagination);
		mav.setViewName("detectfeature/listDetectfeatures.jsp");
		return mav;
	}

	public String listDetectfeatures() {
		return "redirect:/listDetectfeatures";
	}

	@RequestMapping("/viewDetectfeature")
	public ModelAndView viewDetectfeature(@RequestParam Integer detectfeatureId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("detectfeature", detectfeatureDAO.findDetectfeatureByPrimaryKey(detectfeatureId));
		mav.setViewName("detectfeature/viewDetectfeature.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteDetectfeatures")
	public ModelAndView confirmDeleteDetectfeatures(
			@RequestParam(value = "deleteDetectfeaturesIds", required = true) Set<Integer> deleteDetectfeaturesIds) {
		ModelAndView mav = new ModelAndView();

		Set<Detectfeature> deleteDetectfeatures = detectfeatureDAO.findDetectfeaturesByIds(deleteDetectfeaturesIds);
		Set<Detectfeature> referencedDetectfeatures = detectfeatureService
				.loadReferenceDetectfeatures(deleteDetectfeatures);
		boolean hasReferenced;
		if (referencedDetectfeatures.size() == 0) {
			mav.addObject("deleteEntitys", deleteDetectfeatures);
			hasReferenced = false;
		} else {
			deleteDetectfeatures.removeAll(referencedDetectfeatures);
			mav.addObject("referencedEntitys", referencedDetectfeatures);
			mav.addObject("deleteEntitys", deleteDetectfeatures);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Detectfeature");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listDetectfeatures");
		return mav;
	}

	@RequestMapping("/deleteDetectfeature")
	public String deleteDetectfeature(@RequestParam Integer detectfeatureId) {
		Detectfeature detectfeature = detectfeatureDAO.findDetectfeatureByPrimaryKey(detectfeatureId);
		detectfeatureService.deleteDetectfeature(detectfeature);
		return "forward:/listDetectfeatures";
	}

	@RequestMapping("/confirmDeleteDetectfeature")
	public ModelAndView confirmDeleteDetectfeature(@RequestParam Integer detectfeatureId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("detectfeature", detectfeatureDAO.findDetectfeatureByPrimaryKey(detectfeatureId));

		mav.setViewName("detectfeature/deleteDetectfeature.jsp");

		return mav;
	}

	@RequestMapping("/deleteDetectfeatures")
	public String deleteDetectfeatures(
			@RequestParam(value = "deleteDetectfeaturesId", required = true) Set<Integer> deleteDetectfeaturesId) {
		detectfeatureService.deleteDetectfeatures(deleteDetectfeaturesId);
		return "forward:/listDetectfeatures";
	}

	@RequestMapping("/editDetectfeature")
	public ModelAndView editDetectfeature(@RequestParam Integer detectfeatureId) {

		ModelAndView mav = new ModelAndView();

		Detectfeature detectfeature = detectfeatureDAO.findDetectfeatureByPrimaryKey(detectfeatureId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("detectfeature", detectfeature);

		// 把所有的引用detectfeature对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(detectfeatureService.loadDetectfeatureObjectReferencedSet(detectfeature));

		// 将编辑页面detectfeature添加到mav中
		mav.setViewName("detectfeature/editDetectfeature.jsp");
		return mav;
	}

	@RequestMapping("/newDetectfeature")
	public ModelAndView newDetectfeature() {
		ModelAndView mav = new ModelAndView();
		Detectfeature detectfeature = new Detectfeature();

		mav.addObject("detectfeature", detectfeature);

		// 把所有的引用detectfeature对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(detectfeatureService.loadDetectfeatureObjectReferencedSet(detectfeature));

		mav.addObject("newFlag", true);
		mav.setViewName("detectfeature/editDetectfeature.jsp");

		return mav;
	}

	@RequestMapping("/saveDetectfeature")
	public ModelAndView saveDetectfeature(@ModelAttribute Detectfeature detectfeature) {

		ModelAndView mav = new ModelAndView();

		Detect detectTemp = detectfeature.getDetect();
		Detect detect = detectTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(detectDAO.findDetectById(detectTemp.getId()));
		Feature featureTemp = detectfeature.getFeature();
		Feature feature = featureTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(featureDAO.findFeatureById(featureTemp.getId()));
		Unit unitTemp = detectfeature.getUnit();
		Unit unit = unitTemp == null ? null : CmiHibernateProxyUtil.deproxy(unitDAO.findUnitById(unitTemp.getId()));

		Detectfeature existingDetectfeature = CmiHibernateProxyUtil
				.deproxy(detectfeatureDAO.findDetectfeatureByPhysicalPrimaryKey(detect.getId(), feature.getId()));

		detectfeature.setDetect(detect);
		detectfeature.setFeature(feature);
		detectfeature.setUnit(unit);
		// 新建记录
		if (detectfeature.getId() == null) {
			if (detectfeature.equals(existingDetectfeature)) {

				mav.addAllObjects(detectfeatureService.loadDetectfeatureClassReferencedSet(detectfeature));
				// mav = editContinue(mav, detectfeature);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("detectfeature/editDetectfeature.jsp");
			} else {
				detectfeatureService.saveDetectfeature(detectfeature);
				mav.setViewName("forward:/listDetectfeatures");
			}
		}
		// 编辑记录
		else {
			Detectfeature originDetectfeature = CmiHibernateProxyUtil
					.deproxy(detectfeatureDAO.findDetectfeatureByPrimaryKey(detectfeature.getId()));
			if (!detectfeature.equals(originDetectfeature) && detectfeature.equals(existingDetectfeature)) {
				mav.addAllObjects(detectfeatureService.loadDetectfeatureClassReferencedSet(detectfeature));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("detectfeature/editDetectfeature.jsp");
			} else {
				detectfeatureService.saveDetectfeature(detectfeature);
				mav.setViewName("forward:/listDetectfeatures");
			}
		}
		return mav;
	}

	@RequestMapping("/detectfeatureController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
